package com.dckeji.shiplock.webapp.controller.rest;

import com.alibaba.fastjson.JSON;
import com.dckeji.shiplock.framework.constants.SystemConstants;
import com.dckeji.shiplock.webapp.controller.BaseController;
import com.dckeji.shiplock.webapp.pojo.Declare;
import com.dckeji.shiplock.webapp.pojo.LogicRsult;
import com.dckeji.shiplock.webapp.service.IDeclareService;
import com.dckeji.shiplock.webapp.util.ReturnMsgUtil;
import com.sun.corba.se.impl.logging.InterceptorsSystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/8/9
 * Time: 9:16
 * 过闸申报
 */
@RestController
@RequestMapping("/rest/decla/")
public class DeclareController extends BaseController {

    @Autowired
    IDeclareService declareService;

    @RequestMapping("commit")
    public LogicRsult declare(@RequestBody Declare declare) {

        LogicRsult logicRsult = new LogicRsult();
        int updateRow = declareService.commitDeclare(declare);
        if (updateRow > 0) {
            logicRsult.setResultCode(SystemConstants.ResultCode.SUCCESS);
            logicRsult.setResultMsg(SystemConstants.ResultMsg.SUCCESS);

        } else {
            logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
            logicRsult.setResultMsg(SystemConstants.ErrorMsg.SYS_ERROR);
        }
        return logicRsult;
    }

    @RequestMapping("query")
    public LogicRsult query(@RequestBody Declare declare) {
        LogicRsult logicRsult = new LogicRsult();

        List<Declare> query = declareService.query(declare.getShipNo(), declare.getIsExamine());
        if (null != query && query.size() > 0) {
            logicRsult.setResultCode(SystemConstants.ResultCode.SUCCESS);
            logicRsult.setResultMsg(SystemConstants.ResultMsg.SUCCESS);
            logicRsult.setObj(query);
        } else {
            logicRsult.setResultCode(SystemConstants.ResultCode.FIAL);
            logicRsult.setResultMsg(SystemConstants.ErrorMsg.NO_DATA);
        }
        return logicRsult;
    }

    @RequestMapping("queryAll")
    public LogicRsult queryDeclareList() {

        List<Declare> declares = declareService.queryDeclareList();
        if (null != declares && declares.size() > 0) {
            System.out.println(JSON.toJSONString(declares, true));
            return ReturnMsgUtil.success(declares);
        } else {
            return ReturnMsgUtil.notData();
        }
    }


}
